package com.jp.knowledge.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class TabHostPagerView extends ViewPager {

    private Context mContext;
    // 导航栏
    private TabHostNavView mTabNav;
    // 屏幕宽度
    private float mScreenWidth;

    private ChangeStatus changeStatus;



    public void setChangeStatus(ChangeStatus changeStatus) {
        this.changeStatus = changeStatus;
    }

    public TabHostPagerView(Context context) {
        super(context);
        this.mContext = context;
    }

    public TabHostPagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

   /**
     * @param navView 关联导航栏视图
     * @param fm      Frame管理器
     * @param list    展示Fragment集合
     */
    public void init(TabHostNavView navView, FragmentManager fm, List<Fragment> list) {
        this.mTabNav = navView;
        this.setOnPageChangeListener(mPageChange);
        this.mScreenWidth = DensityUtil.getScreenWidth();
        this.mTabNav.setOnTabSelectListener(onTabSelect);
        if (list.isEmpty()) return;
        this.setAdapter(new TabFragmentAdapter(fm, list));
    }

    /**
     * 卡片内容切换监听
     */
    OnPageChangeListener mPageChange = new OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            if (mTabNav == null) return;

            mTabNav.setIndicatorLinePosition((int) ((position+positionOffset)*(mScreenWidth/getAdapter().getCount())));
            if (mTabNav != null)
                mTabNav.refreshTab((int) (position+positionOffset*2));
        }

        @Override
        public void onPageSelected(int position) {
//            if (mTabNav == null) return;
//            mTabNav.refreshTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    /**
     * 导航栏点击项监听（自定义监听接口）
     */
    TabHostNavView.OnTabSelectListener onTabSelect = new TabHostNavView.OnTabSelectListener() {
        @Override
        public void onTabSelect(int position) {
            setCurrentItem(position);
        }
    };

    /**
     * 卡片内容适配器
     */
    public static class TabFragmentAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList = new ArrayList<>();

        public TabFragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }
    }

    public interface ChangeStatus {
        public void change(int position);
    }


    private int startX;
    private int startY;
    private boolean intercepted = false;

    /**
     * 拦截事件
     *
     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        intercepted = false;
//        switch (ev.getAction()) {
//            //父容器不需要拦截
//            case MotionEvent.ACTION_DOWN:
//                startX = (int) ev.getX();
//                startY = (int) ev.getY();
//                break;
//            //在此判断父容器是否需要拦截
//            case MotionEvent.ACTION_MOVE:
//                int difX = (int) Math.abs(ev.getX() - startX);
//                if (Math.abs(ev.getY() - startY) < Math.abs(ev.getX() - startX) && difX > 20) {
//                    intercepted = true;
//                }
//                //父容器不需要拦截
//            case MotionEvent.ACTION_UP:
//                difX = (int) Math.abs(ev.getX() - startX);
//                if (Math.abs(ev.getY() - startY) < Math.abs(ev.getX() - startX) && difX > 20) {
//                    intercepted = true;
//                }
//            default:
//                break;
//
//        }
//        if (intercepted) {
//            return true;
//        } else {
            return super.onInterceptTouchEvent(ev);
//        }
    }


}
